package news.controller;

import com.alibaba.fastjson.JSON;
import news.example.Topic;
import news.service.ISelTopicService;
import news.service.SelTopicServiceImpl;
import news.util.BaseHttpServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author JiangJ
 * @date 2022/2/28 20:14
 * 查询数据
 */
@WebServlet("/SelTopic")
public class SelTopicController extends BaseHttpServlet {
    private final ISelTopicService iSelTopicService = new SelTopicServiceImpl();
    private static final long serialVersionUID = 1L;

    /**
     * 获取主题
     */
    public void getTopicName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        request.setCharacterEncoding("utf-8");

        PrintWriter writer = response.getWriter();

        List<Topic> list = iSelTopicService.getTopicName();

        String resJson = JSON.toJSONString(list);

        writer.write(resJson);

        writer.flush();
        writer.close();
    }

    /**
     * 获取Topic数据
     */
    public void queryTopic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应内容类型
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("utf-8");
        //1.接受参数
        //2.查询数据
        iSelTopicService.findAllTopic();

        //3.返回成功响应

        request.getRequestDispatcher(request.getContextPath() + "/news/suc.jsp").forward(request, response);
    }

}
